package com.struts.action.impl;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.struts.action.IAction;
import com.struts.pojo.BaseFormVo;
import com.struts.pojo.XmlVo;
import com.struts.utis.ReflectAutoBeanUtis;
import com.struts.utis.XmlServiceLoadUtis;



public class ActionServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Map<String, Object>  paramMap = new HashMap<String, Object>();
		  String  module=null;//要调用的service
	      String  method=null;//要调用的方法
	        
        Map<String, String[]>  params =request.getParameterMap();
        Iterator<String> keyIterator = (Iterator) params.keySet().iterator();
       
        while (keyIterator.hasNext()) {
            String key = (String) keyIterator.next();
            if(key.equals("module")){
            	module= request.getParameter("module");//要调用的service
            	continue;
            }else if(key.equals("method")){      
            	method = request.getParameter("method");
            	continue;
            }else{
            	 String value = params.get(key)[0];
                 paramMap.put(key, value);
            }
           
        }
		
		//请求路轻 http://localhost:8080/struts/index.jsp /sss.do
		String path = this.getPath(request.getServletPath());
		//从上下文中获取内容
		@SuppressWarnings("unchecked")
		Map<String, XmlVo> map = (Map<String, XmlVo>) this.getServletContext().getAttribute("struts");
		
		XmlVo bean = map.get(path);
		//要注入的vo实体类
		String formName = bean.getForm();
		//调用的action
		String type = bean.getType();
		
		//通过反正注入属性
		BaseFormVo actionForm = ReflectAutoBeanUtis.autoInjection(formName, request); 
		IAction action = null;
		Map<String, String> mapc = new HashMap<String, String>();
		
		String url = "";
		/*try {//通过反正调用接口
			Class<?> clazz = Class.forName(type);
			//action = (IAction)clazz.newInstance();
			//url = action.exeute(request, actionForm, bean.getForward());
			Object object = clazz.newInstance();
			Method m1 = clazz.getDeclaredMethod("exeute",HttpServletRequest.class, BaseFormVo.class, Map.class);
			
			url= (String) m1.invoke(object,request, actionForm, bean.getForward()); 
			
		}catch(Exception e) {
			e.printStackTrace();
		}*/
		
		Map<String, Method> methodMapc=XmlServiceLoadUtis.methodMap;
       
        
        
    	
		try {
			Class clazz = Class.forName(XmlServiceLoadUtis.beansMap.get(module));
			Object obj = clazz.newInstance();
			//Method[] methods= clazz.getMethods();
	        Method met =methodMapc.get(module+"."+method);
	        url= (String)met.invoke(obj,paramMap);
		} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		RequestDispatcher requestDispatcher = request.getRequestDispatcher(bean.getForward().get(url));
		requestDispatcher.forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}
	
	private String getPath(String path) {
		return path.split("\\.")[0];
	}
}
